﻿using AgentSystem.AgentCreator.Util;
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AgentSystem.AgentCreator.Sbpel.Activities
{
    /// <summary>
    /// Class representing a collection of variables ~ the agent's state.
    /// </summary>
    public class VariablesActivity : AbstractActivity
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="parent">The parent</param>
        public VariablesActivity(AbstractActivity parent) : base(parent) {}

        /// <summary>
        /// Get the Code Dom.
        /// Create the Agent's State Class.
        /// </summary>
        /// <returns></returns>
        public override System.CodeDom.CodeObject[] GetCodeDom()
        {
            IList<CodeObject> codeObjectsToReturn = new List<CodeObject>();
            var stateClassName = Utils.UppercaseFirst(Utils.RemoveSpecialCharacters(ParentActivity.Name));
            string agentStateClassName = string.Format("Agent{0}State", stateClassName);

            CodeTypeDeclaration stateClass = new CodeTypeDeclaration(agentStateClassName);
            stateClass.Attributes = MemberAttributes.Public;
            stateClass.CustomAttributes.Add(new CodeAttributeDeclaration("Serializable"));

            codeObjectsToReturn.Add(stateClass);

            foreach (var child in ChildActivities)
            {
                foreach (var codeObject in child.GetCodeDom())
                {
                    if (codeObject is CodeMemberField)
                    {
                        stateClass.Members.Add(codeObject as CodeTypeMember);
                    }
                    else
                    {
                        codeObjectsToReturn.Add(codeObject);
                    }
                }
            }

            return codeObjectsToReturn.ToArray();
        }
    }
}
